package com.cate.service.Impl;

import com.cate.context.BaseContext;
import com.cate.exception.BusinessException;
import com.cate.mapper.AddressBookMapper;
import com.cate.pojo.entity.AddressBook;
import com.cate.result.SystemCode;
import com.cate.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @CreateTime: 2023/8/1
 * @Author: <==lhd==>
 * @Description:
 */
@Service
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Override
    public void deleteById(Long id) {
        final int i = addressBookMapper.delete(id);
        if(i<=0){
            throw new BusinessException(SystemCode.REQUEST_ERROR,"删除地址失败");
        }
    }

    @Override
    public void updateById(AddressBook addressBook) {
        final int i = addressBookMapper.updateById(addressBook);
        if(i<=0){
            throw new BusinessException(SystemCode.REQUEST_ERROR,"更新地址失败");
        }
    }

    @Override
    public void save(AddressBook addressBook) {
        final Long currentId = BaseContext.getCurrentId();
        addressBook.setUserId(currentId);
        addressBook.setIsDefault(0);
        final int i = addressBookMapper.insert(addressBook);
        if(i<=0){
            throw new BusinessException(SystemCode.REQUEST_ERROR,"添加地址失败");
        }
    }

    @Override
    public AddressBook getDefaultAddress() {
        final Long currentId = BaseContext.getCurrentId();
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(1);
        addressBook.setUserId(currentId);
        final List<AddressBook> list = addressBookMapper.list(addressBook);
        if(list ==null || list.isEmpty()){
            throw new BusinessException(SystemCode.REQUEST_ERROR,"未设置默认地址");
        }
        return list.get(0);
    }

    @Override
    @Transactional
    public void setDefaultAddress(AddressBook addressBook) {
        final Long currentId = BaseContext.getCurrentId();

        // 将其他地址设置为非默认
        AddressBook address = new AddressBook();
        address.setIsDefault(0);
        address.setUserId(currentId);
        addressBookMapper.updateIsDefaultByUserId(address);

        addressBook.setUserId(currentId);
        addressBook.setIsDefault(1);
        final int i = addressBookMapper.updateById(addressBook);
        if(i<=0){
            throw new BusinessException(SystemCode.REQUEST_ERROR,"设置默认地址失败");
        }
    }

    @Override
    public List<AddressBook> list() {
        final Long currentId = BaseContext.getCurrentId();
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(currentId);
        final List<AddressBook> list = addressBookMapper.list(addressBook);
        if(list==null || list.isEmpty()){
            throw new BusinessException(SystemCode.REQUEST_ERROR,"获得地址列表失败");
        }
        return list;
    }

    @Override
    public AddressBook getById(Long id) {
        final AddressBook addressBook = addressBookMapper.selectById(id);
        if(addressBook==null){
            throw new BusinessException(SystemCode.REQUEST_ERROR,"查询地址信息失败");
        }
        return addressBook;
    }
}
